
/* The Single-Server Queue System */

/* External definitions for single-server queue system */

#include <stdio.h>
#include <math.h>
#include "rand.h"	/* header file for random-number generator. */

#define Q_LIMIT 100	/* Limit on queue length */
#define BUSY 	1	/* Mnemonics for server's being busy */
#define IDLE	0	/* and idle */
FILE *fp;

int	next_event_type, num_custs_delayed, num_delays_required, num_events,
	num_in_q, server_status, total_arrived, total_departed;
float	area_num_in_q, area_server_status, mean_interarrival, mean_service,
	current_time, time_arrival[Q_LIMIT + 1], time_last_event, 
	time_next_event[3], total_of_delays, LINK_BANDWIDTH;

void	initialize (void);
void	timing(void);
void	arrive(void);
void	depart(void);
void	report(void);
void	update_time_avg_stats(void);
float	expon(float mean);

main(int argc, char *argv[])	/* main function */
{

    int i;
total_arrived=0;
 total_departed=0;
fp=fopen("TimeVsBandwidth","w+");
fclose(fp);
    for (i = 1; i < argc; i++)
        puts(argv[i]);
   /* specify the number of events for the timing function */
   num_events = 2;
LINK_BANDWIDTH=0;
   /* read input parameters */
   printf("Input mean interarrival, mean service and no. of delays required\n");
   //scanf("%f %f %d", &mean_interarrival, &mean_service,             &num_delays_required);
  
mean_interarrival=atoi(argv[1]);
mean_service=atoi(argv[2]);
num_delays_required=atoi(argv[3]);
   /* write report heading and input parameters */
   printf("	Bandwidth utilization program\n\n");
   printf("Mean interarrival time%11.3f minutes\n\n", mean_interarrival);
   printf("Mean life time for each call%16.3f minutes\n\n", mean_service);
   printf("Number of customers%14d\n\n", num_delays_required);

   /* Initialise the simulation */

   initialize();
 
   /* run the simulation while more delays are still needed */
   while (num_custs_delayed < num_delays_required) 
   {
    printf(" main function arrived %d current time %d \n",num_custs_delayed,current_time);
     /* determine the next event */
     timing();

     /* update time-average statistical accumulators */
     update_time_avg_stats();

     /* invoke the appropriate event function */
     switch (next_event_type)
     {
        case 1: arrive();
                break;
        case 2: depart();
                break;
     }
   }

   /* invoke the report generator and end the simulation */
   report();
 
   return 0;
}


void initialize(void) /* initialization function */
{
   /* initialise the simulation clock */
   current_time = 0.0;

   /* initialise the state variables */

   server_status = IDLE;
   num_in_q = 0;
   time_last_event = 0.0;
 
   /* initialise the statistical counters */

   num_custs_delayed = 0;
   total_of_delays = 0.0;
   area_num_in_q = 0.0;
   area_server_status = 0.0;

   /* initialise event list. Since no customers are present, the departure
      (service completion) event is eliminated from consideration */
   time_next_event[1] = current_time + expon(mean_interarrival);
   time_next_event[2] = 1.0e+30;
}


void timing(void) /* timing function */
{
   int i;
   float min_time_next_event = 1.0e+29;
   
   next_event_type = 0;

   /* determine the event type of the next event to occur */
 
   for (i=1; i <= num_events; ++i)
   {
     if (time_next_event[i] < min_time_next_event)
       {
         min_time_next_event = time_next_event[i];
         next_event_type = i;
       }
   }
   
   /* check to see whether the event list is empty */
   if (next_event_type == 0)
     {
       /* the event list is empty, so stop the simulation */
       printf("\nEvent list empty at time %f", current_time);
       exit(1);
     }

   /* the list is not empty, so advance the simulation clock */
   current_time = min_time_next_event;
printf("timing function: current time %10.3f next event type %d\n",current_time,next_event_type);
}


void arrive(void) /* Arrival event function */
{
  float delay;

  /* schedule next arrival */
  time_next_event[1] = current_time + expon(mean_interarrival);

  total_arrived++;
    time_arrival[num_in_q] = current_time;


    /* server is idle, so arriving customer has a delay of zero 
       (the following two statements are for program clarity and do not 
       affect the results of the simulation)  */   
    delay = 0.0;
    total_of_delays += delay;

    /* increment the no. of customers delayed, and make server busy */
    ++num_custs_delayed;
 LINK_BANDWIDTH=LINK_BANDWIDTH+2;
   
fp=fopen("TimeVsBandwidth","a+");
fprintf(fp,"%10.3f %d \n",current_time,LINK_BANDWIDTH);
fclose(fp);
 /* schedule a departure (service completion) */
    time_next_event[2] = current_time + expon(mean_service);
printf("Call Arrival Number of calls %d Link bandwidth %10.3f current time %10.3f scheduled end%10.3f\n",num_custs_delayed,LINK_BANDWIDTH,current_time,time_next_event[2]);

}



void depart(void) /* Departure event function */
{
  int i;
  float delay;

total_departed++;

LINK_BANDWIDTH=LINK_BANDWIDTH-2;
    /* compute the delay of customer who is beginning service and update 
       the total delay accumulator */
    delay = current_time - time_arrival[1];
    total_of_delays += delay;
    
    /* increment the no. of customers delayed, and schedule departure */
    //num_custs_delayed--;
    time_next_event[2] = current_time + expon(mean_service);

printf(" Call Drop Number of calls %d Link bandwidth %10.3f current time %10.3f\n",num_custs_delayed,LINK_BANDWIDTH,current_time);

}


void report(void) /* report generator function */
{
printf("Total arrived %d, Total Departed %d",total_arrived,total_departed);
   /* compute and write estimates of desired measures of performance */

  /* printf("\nAverage delay in queue%11.3f minutes\n\n",
      total_of_delays/ num_custs_delayed);
   printf("Average number in queue%10.3f\n\n", area_num_in_q/current_time);
   printf("Server Utilization%15.3f\n\n", area_server_status/current_time);
   printf("Time simulation ended%12.3f\n", current_time);
*/}


void update_time_avg_stats(void) /* update area accumulators for time-average
                                    statistics */
{
   float time_since_last_event;

   /* Compute time since last event, and update last-event-time marker */
   time_since_last_event = current_time - time_last_event;
   time_last_event = current_time;

   /* update area under number-in-queue function */
   area_num_in_q += num_in_q * time_since_last_event;
 
   /* update area under server-busy indicator function */
   area_server_status += server_status * time_since_last_event;
}


float expon(float mean) /* exponential variate generation function */
{
  float u;

  /* generate a U(0,1) random veriate */
  u = randno(1);

  /* return an exponential random variate with mean "mean" */
  return -mean*log(u);
}



 

       

  
   



